package com.pulaien.shop.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.system.domain.OrderLabel;
import com.pulaien.system.domain.dto.OrderLabelReq;
import com.pulaien.system.service.OrderLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuanlai
 * @create 2024-01-18 16:03
 **/
@RestController
@RequestMapping("/shop/order/label" )
@Api(tags = "商城端订单标签配置")
public class OrderLabelController {


    @Autowired
    private OrderLabelService orderLabelService;


    @ApiOperation(value = "客户配置标签")
    @PostMapping("/config/label")
    public AjaxResult configLabel(@RequestBody OrderLabelReq req) {

        Long orderId = req.getOrderId();
        QueryWrapper<OrderLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id" , orderId);
        boolean remove = orderLabelService.remove(queryWrapper);

        List<OrderLabel> list = new ArrayList<>();
        req.getLabelList().forEach(
                s -> {
                    OrderLabel ol = new OrderLabel();
                    ol.setOrderId(orderId);
                    ol.setLabel(s);
                    list.add(ol);
                }
        );

        boolean b = orderLabelService.saveBatch(list);
        return AjaxResult.success();
    }


    @ApiOperation(value = "根据订单号获取订单下面的标签")
    @GetMapping("/getOrderLabel")
    public AjaxResult getOrderLabel(@RequestParam("orderId") Long orderId )  {

        QueryWrapper<OrderLabel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id" , orderId);
        List<OrderLabel> list = orderLabelService.list(queryWrapper);


        OrderLabelReq orderLabelReq = new OrderLabelReq();
        orderLabelReq.setOrderId(orderId);
        List<String> labelList = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(list)) {

            list.forEach(
                    s -> {
                        labelList.add(s.getLabel());
                    }
            );
            orderLabelReq.setLabelList(labelList);

        }

        return AjaxResult.success(orderLabelReq);
    }
}
